﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author :Zjw
 * CreateTime : 2019-09-19
 * 摄像机自由移动脚本
 */
public class FreeCamera : MonoBehaviour
{
    public static FreeCamera instance;
    public Texture2D m_Curror;//鼠标指针
    public float m_MoveSpeed = 0.2f;//WASD移动速度
    public float m_DragSpeed = 0.2f;//中间拖动速度
    //旋转变量;
    private float m_deltX = 0f;
    private float m_deltY = 0f;
    //缩放变量;
    private float m_distance = 10f;
    public float m_RotateSpeed = 1f;
    //移动变量;
    private Vector3 m_mouseMovePos = Vector3.zero;

    private float initAxisX = 0f;//初始化X轴
    private float initAxisY = 0f;//初始化Y轴
    void Awake()
    {
        instance = this;
    }
    void Start()
    {
        initAxisX = this.transform.localEulerAngles.x;
        initAxisY = this.transform.localEulerAngles.y;
        //初始化就按下右键，防止摄像机第一次按下右键时的跳动
        m_deltX += Input.GetAxis("Mouse X") * m_RotateSpeed;
        m_deltY -= Input.GetAxis("Mouse Y") * m_RotateSpeed;
        m_deltY += initAxisX;
        m_deltX += initAxisY;
        m_deltX = ClampAngle(m_deltX, -360, 360);
        m_deltY = ClampAngle(m_deltY, -90, 90);
        this.transform.localRotation = Quaternion.Euler(m_deltY, m_deltX, 0);
    }

    void Update()
    {
        //右键--旋转
        if (Input.GetMouseButton(1))
        {
            m_deltX += Input.GetAxis("Mouse X") * m_RotateSpeed;
            m_deltY -= Input.GetAxis("Mouse Y") * m_RotateSpeed;
            m_deltX = ClampAngle(m_deltX, -360, 360);
            m_deltY = ClampAngle(m_deltY, -90, 90);
            GetComponent<Camera>().transform.rotation = Quaternion.Euler(m_deltY, m_deltX, 0);
        }
        //滚轮--移动
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //自由缩放方式;
            m_distance = Input.GetAxis("Mouse ScrollWheel") * 1f;
            GetComponent<Camera>().transform.localPosition = GetComponent<Camera>().transform.position + GetComponent<Camera>().transform.forward * m_distance;
        }
        //中键--移动                                                                                                      
        else if (Input.GetMouseButton(2))
        {
            Cursor.SetCursor(m_Curror, Vector2.zero, CursorMode.Auto);
            float mx = -Input.GetAxis("Mouse X") * m_DragSpeed;
            float move_x = mx;
            float my = -Input.GetAxis("Mouse Y") * m_DragSpeed;
            float angleX = transform.localEulerAngles.x * Mathf.PI / 180.0f;
            float move_y = my * Mathf.Sin(angleX);
            float move_z = my * Mathf.Cos(angleX);
            this.transform.Translate(move_x, move_y, move_z);
        }
        else if (Input.GetMouseButtonUp(2))
        {
            Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
        }
        //WASD--移动
        MoveCamera();
    }

    //规划角度;
    float ClampAngle(float angle, float minAngle, float maxAgnle)
    {
        if (angle <= -360)
            angle += 360;
        if (angle >= 360)
            angle -= 360;

        return Mathf.Clamp(angle, minAngle, maxAgnle);
    }
    //wasd控制移动
    private void MoveCamera()
    {
        float translationZ = Input.GetAxis("Vertical") * m_MoveSpeed;
        transform.localPosition += this.transform.localRotation * new Vector3(0, 0, translationZ);

        float translationX = Input.GetAxis("Horizontal") * m_MoveSpeed;
        transform.localPosition += this.transform.localRotation * new Vector3(translationX, 0, 0);

        //限制高度位置
        float posY = Mathf.Clamp(this.transform.position.y, 1, 500);
        this.transform.position = new Vector3(this.transform.position.x, posY, this.transform.position.z);
    }
}